import javax.swing.*;
import javax.swing.tree.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

final class OpenItem extends AbstractAction{
  private final Doer frame;
  private JFileChooser files;
  OpenItem(String name,Doer frame){
    super(name);
    this.frame=frame;
    putValue(SHORT_DESCRIPTION,name);
    String iconFileName="images"+File.separator+name+".gif";
    if(new File(iconFileName).exists())putValue(SMALL_ICON,new ImageIcon(iconFileName));
  }

  public void actionPerformed(ActionEvent e){
    openFile();
  }

  public void openFile(){
    try{
      files=new JFileChooser(new File(frame.getDirection()));
      File file=showDialog("Open File");

      if(file!=null){
        frame.setDirection(file.getParent());
	frame.getStatusBar().getSystemDirection().setText("D: "+file.getParent());
	int i=0;
	while(i<=frame.getTab().getTabCount()-1){

	  if(frame.getTab().getTitleAt(i).equals(file.getName())){//name=file.getName();30
            frame.getTab().setSelectedIndex(i);
            //save
	    InterWindow interWindow=(InterWindow)(frame.getTab().getComponentAt(i));
	    if(interWindow.isSaved()==false)
	      if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(frame,
	                                                         "The file does'nt save.Save?",
								 "File does'nt save",
	                                                         JOptionPane.YES_NO_OPTION,
		                                                 JOptionPane.WARNING_MESSAGE))
	        frame.getSaveItem().saveFile();
	      //read
	    JTextArea text=interWindow.getTextArea();
            text.setText("");
	    BufferedReader in=new BufferedReader(new FileReader(file));
	    String s=new String();
            while(true){
	      s=in.readLine();
	      if(s==null)break;
              text.append(s+'\n');
            }
	    in.close();
	    frame.setTitle(file.getName());
	    interWindow.setSaved(true);
	    frame.getSaveItem().setEnabled(false);
	    interWindow.setDirection(file.getParent());
	    frame.getStatusBar().getCD().setText("cd: "+file.getParent());
	    return;
	  }//name=file.getName();61
	  i++;
	}
        if(file.exists()){//exists
	  InterWindow interWindow=new InterWindow(frame);
	  JTextArea text=interWindow.getTextArea();
	  BufferedReader in=new BufferedReader(new FileReader(file));
	  String s=new String();
          while(true){
	    s=in.readLine();
	    if(s==null)break;
            text.append(s+'\n');
          }
	  in.close();
	  frame.getObs().addObserver((Observer)interWindow);
	  frame.getTab().addTab(file.getName(),interWindow);
	  frame.getTab().setSelectedIndex(frame.getTab().getTabCount()-1);
	  frame.getSaveItem().setEnabled(false);
	  interWindow.setSaved(true);
          frame.setTitle(file.getName());
	DefaultMutableTreeNode node=new DefaultMutableTreeNode(file.getName());
	frame.getFileTree().getRoot().add(node);
	frame.getFileTree().getTree().updateUI();
        }else{//not exists
	  if(JOptionPane.NO_OPTION==JOptionPane.showConfirmDialog(frame,
	    "The file does'nt exist.Create?",
	    "File does'nt exist",
	    JOptionPane.YES_NO_OPTION,
	    JOptionPane.WARNING_MESSAGE))return;
          frame.getNewItem().newFile(file.getName());
        }
      }
      if(frame.getTab().getTabCount()>0){
        frame.getSaveAsItem().setEnabled(true);
	frame.getSaveAllItem().setEnabled(true);
	frame.getCloseItem().setEnabled(true);
	frame.getCloseAllItem().setEnabled(true);
	frame.getFindItem().setEnabled(true);
	frame.getFindNextItem().setEnabled(true);
	frame.getCompileItem().setEnabled(true);
	frame.getRunItem().setEnabled(true);
        frame.getCopyItem().setEnabled(true);
	frame.getCutItem().setEnabled(true);
	frame.getPasteItem().setEnabled(true);
	frame.getUndoItem().setEnabled(true);
	frame.getRedoItem().setEnabled(true);
      }
    }catch(IOException e){
      System.out.println(e);
    }
  }

  public File showDialog(String title){
      files.setDialogTitle(title);
      files.setFileSelectionMode(files.FILES_ONLY);
      files.setCurrentDirectory(new File(frame.getDirection()));
      files.rescanCurrentDirectory();

      Ext javaFilter=new Ext(".java","Java files(*.java)");

      files.addChoosableFileFilter(javaFilter);
      files.setFileFilter(javaFilter);

      int result=files.showDialog(frame,null);

      return (result==files.APPROVE_OPTION)?files.getSelectedFile():null;
    }
  }
